package com.such.kit.validate.validator.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.such.kit.placeholder.PlaceholderCommons;
import com.such.kit.string.StringCommons;
import com.such.kit.validate.validator.Validator;

/**
 * @author SUCH
 * <pre>
 * 数据取值范围校验器
 * 
 * [参数集说明]
 * 输入内容只能在范围内取值：#[1], #[2], #[3] ...
 * </pre>
 */
public class DataRangeValidator extends Validator {

	@Override
	protected boolean checkParams(List<String> params) {
		if (params.size() > 0) {
			return true;
		}
		return false;
	}

	@Override
	protected boolean verify(String value, List<String> params) {
		Set<String> checker = new HashSet<String>(params);
		if (checker.contains(value)) {
			return true;
		}
		return false;
	}

	@Override
	protected String getVerifyMessage(List<String> params) {
		int paramsSize = params.size();
		String message = "";
		if (paramsSize > 10) {
			message = StringCommons.merger("输入内容只能在范围内取值（可取值数 [", paramsSize, "]，展示前 [10] 个）：", PARAMS_JSON_KEY);
		} else {
			message = StringCommons.merger("输入内容只能在范围内取值：", PARAMS_JSON_KEY);
		}
		return message;
	}

	@Override
	protected String buildMessage(String value, List<String> params) {
		int paramsSize = params.size();
		Map<String, String> paramsMap = new HashMap<String, String>();
		if (paramsSize > 10) {
			paramsMap.put(PARAMS_JSON, StringCommons.merger(params.subList(0, 25).toArray(), ", ", true));
		} else {
			paramsMap.put(PARAMS_JSON, StringCommons.merger(params.toArray(), ", ", true));
		}
		return PlaceholderCommons.replacePlaceholder(getVerifyMessage(params), paramsMap);
	}

}
